package com.cqtd.common.dict.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cqtd.common.base.service.FooGenericServiceImpl;
import com.cqtd.common.dict.dao.CqSystemDictTypeDao;
import com.cqtd.common.dict.model.CqSystemDictType;
import com.cqtd.common.dict.service.CqSystemDictTypeService;
import com.googlecode.ehcache.annotations.Cacheable;
import com.googlecode.ehcache.annotations.KeyGenerator;
import com.googlecode.ehcache.annotations.Property;
import com.googlecode.ehcache.annotations.TriggersRemove;
import com.googlecode.ehcache.annotations.When;
import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;

@Service
public class CqSystemDictTypeServiceImpl extends FooGenericServiceImpl<CqSystemDictType> implements
		CqSystemDictTypeService {
	protected Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	@Autowired
	public void setCqSystemDictTypeDao(CqSystemDictTypeDao cqSystemDictTypeDao) {
		setFooGenericDao(cqSystemDictTypeDao);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cqtd.common.dict.service.CqSystemDictTypeService#getDictByDictId(java.lang.String)
	 * 查询并根据方法参数生成ID插入缓存
	 */
	@Cacheable(cacheName = "DICT_CACHE",
			   keyGenerator = @KeyGenerator(
					   name = "HashCodeCacheKeyGenerator", 
					   properties = @Property(name="includeMethod", value="true")    
			   )
	)
	public CqSystemDictType getDictByDictId(String dictId) {
		CqSystemDictType cqSystemDictType = new CqSystemDictType();
		Search search = new Search();
		search.addFilterEqual("dictId", dictId);
		cqSystemDictType = this.getFooGenericDao().searchUnique(search);
		return cqSystemDictType;
	}
	
	@TriggersRemove(
			 cacheName = "DICT_CACHE",
			 when = When.AFTER_METHOD_INVOCATION,
			 removeAll = true
	)
	public void remove(List<CqSystemDictType> list){
		this.getFooGenericDao().remove(list);
	}
	
	@TriggersRemove(
			 cacheName = "DICT_CACHE",
			 when = When.AFTER_METHOD_INVOCATION,
			 removeAll = true
	)
	public boolean remove(CqSystemDictType cqSystemDictType){
		return this.getFooGenericDao().remove(cqSystemDictType);
	}
	
	@TriggersRemove(
			 cacheName = "DICT_CACHE",
			 when = When.AFTER_METHOD_INVOCATION,
			 removeAll = true
	)
	public void saveCqSystemDictType(CqSystemDictType... cqSystemDictType) {
			this.getFooGenericDao().save(cqSystemDictType);
	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.cqtd.common.dict.service.CqSystemDictTypeService#findAllDictByModule(java.lang.String)
	 */
	public SearchResult<CqSystemDictType> findAllDictByModule(String moduleName) {
		Search search = new Search();
		search.addFilterEqual("moduleName", moduleName);
		SearchResult<CqSystemDictType> result = this.searchAndCount(search);
		return result;
	}
	
	
	/*
	 * 	(non-Javadoc)
	 * @see com.cqtd.common.dict.service.CqSystemDictTypeService#id2Name(java.lang.String)
	 */
	
	@Cacheable(
			   cacheName = "DICT_CACHE",
			   keyGenerator = @KeyGenerator(
					   name = "HashCodeCacheKeyGenerator", 
					   properties = @Property(name="includeMethod", value="true")    
			   )
	)
	public String id2Name(String dictId) {
		String name = "";
		CqSystemDictType cqSystemDictType = new CqSystemDictType();
		Search search = new Search();
		search.addFilterEqual("dictId", dictId);
		cqSystemDictType = this.getFooGenericDao().searchUnique(search);
		if(cqSystemDictType!=null){
			name = cqSystemDictType.getDictName();
		}
		return name;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cqtd.common.dict.service.CqSystemDictTypeService#getDictByParentDictId(java.lang.String)
	 */
	@Cacheable(
			   cacheName = "DICT_CACHE",
			   keyGenerator = @KeyGenerator(
					   name = "HashCodeCacheKeyGenerator", 
					   properties = @Property(name="includeMethod", value="true")    
			   )
	)
	public List<CqSystemDictType> getDictByParentDictId(String parentDictId) {
		Search search = new Search();
		search.addFilterEqual("parentDictId", parentDictId);
		search.addSortAsc("sort");
		return this.getFooGenericDao().search(search);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cqtd.common.dict.service.CqSystemDictTypeService#getDictByParentDictId(java.lang.String)
	 */
	@Cacheable(
			   cacheName = "DICT_CACHE",
			   keyGenerator = @KeyGenerator(
					   name = "HashCodeCacheKeyGenerator", 
					   properties = @Property(name="includeMethod", value="true")    
			   )
	)
	public List<CqSystemDictType> getDictByParentDictIdWithLike(String parentDictId) {
		Search search = new Search();
		search.addFilterLike("parentDictId", parentDictId+"%");
		return this.getFooGenericDao().search(search);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cqtd.common.dict.service.CqSystemDictTypeService#findAllDict()
	 * 清除所有缓存
	 */
	@TriggersRemove(
			 cacheName = "DICT_CACHE",
			 when = When.AFTER_METHOD_INVOCATION,
			 removeAll = true
	)
	public void clearAllDict() {
		//清除所有缓存
	}
	
	
}
